package com.android.fragmentdemo
import android.content.res.Resources
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.core.view.get
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import com.android.fragmentdemo.adapters.ViewPager2UseAdapter
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityViewPager2UseBinding
import com.android.fragmentdemo.viewpager2.ViewPager2Transformer

class ViewPager2UseActivity : BaseActivity<ActivityViewPager2UseBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_view_pager2_use

    override fun init(savedInstanceState: Bundle?) {
        val dataSet = DataFactoryIp.createDataSet()
        val mAdapter = ViewPager2UseAdapter()
        mAdapter.itemDataSet = dataSet
        mBinding.viewPager2.apply {
            (get(0) as RecyclerView).apply {
                overScrollMode = View.OVER_SCROLL_NEVER
                setPadding(32.px, 0, 32.px, 0)
                clipToPadding = false
            }
            offscreenPageLimit = 3
            adapter = mAdapter
            setPageTransformer(CompositePageTransformer().apply {
                addTransformer(MarginPageTransformer(0))
                addTransformer(ViewPager2Transformer())
            })
        }
    }

    companion object {
        val Int.px: Int
            get() = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                this.toFloat(),
                Resources.getSystem().displayMetrics
            ).toInt()
    }
}